home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / software-center / softwarecenter / utils.py < prev    next >
Text File  |  2009-10-23  |  2KB  |  55 lines

  1. # Copyright (C) 2009 Canonical
  2. #
  3. # Authors:
  4. #  Michael Vogt
  5. #
  6. # This program is free software; you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation; version 3.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18.  
  19.  
  20. class ExecutionTime(object):
  21.     """
  22.     Helper that can be used in with statements to have a simple
  23.     measure of the timming of a particular block of code, e.g.
  24.     with ExecutinTime("db flush"):
  25.         db.flush()
  26.     """
  27.     def __init__(self, info=""):
  28.         self.info = info
  29.     def __enter__(self):
  30.         self.now = time.time()
  31.     def __exit__(self, type, value, stack):
  32.         print "%s: %s" % (self.info, time.time() - self.now)
  33.  
  34.  
  35.  
  36. def encode_for_xml(unicode_data, encoding="ascii"):
  37.     """ encode a given string for xml """
  38.     return unicode_data.encode(encoding, 'xmlcharrefreplace')
  39.  
  40. def decode_xml_char_reference(s):
  41.     """ takes a string like 
  42.         'Search…' 
  43.         and converts it to
  44.         'Search...'
  45.     """
  46.     import re
  47.     p = re.compile("\&\#x(\d\d\d\d);")
  48.     return p.sub(r"\u\1", s).decode("unicode-escape")
  49.     
  50. if __name__ == "__main__":
  51.     s = decode_xml_char_reference('Search…')
  52.     print s
  53.     print type(s)
  54.     print unicode(s)
  55.